package com.bite.plane;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/*
* 游戏窗口
* */
public class MyGameFrame extends Frame {

    Image plane = GameUtil.getImage("images/plane.png");
    Image bg = GameUtil.getImage("images/bg.jpg");

    static int count = 0;
    int x = 0;
    //画笔
    @Override
    public void paint(Graphics g) {  // g 当作画笔
        System.out.println("窗口被画了 "+count+" 次");
        g.drawImage(bg,0,0,800,800,null);
        g.drawImage(plane,x,300,20,20,null);
        x += 10;
        count++;
    }

    //初始化窗口
    public void launchFrame() {
        this.setTitle("飞机大战");
        setVisible(true);

        setSize(500,500);
        setLocation(500,100);

        //关闭窗口
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //正常退出
                System.exit(0);
            }
        });

        //启动重画窗口
        new PaintThread().start();
    }

    //线程调用,重复调用
    class PaintThread extends Thread {
        public void run() {
            while (true) {
                repaint();

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static void main(String[] args) {
        MyGameFrame myGameFrame = new MyGameFrame();
        myGameFrame.launchFrame();
    }

}
